package com.data.modules.openapi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;

import com.data.modules.openapi.entity.OpenApiApp;
import com.data.modules.openapi.entity.OpenApiAppPermission;
import com.data.modules.openapi.service.IOpenApiAppPermissionService;
import com.data.modules.openapi.service.IOpenApiAppService;
import com.data.modules.system.vo.MagicPathVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: open_api_app
 * @Author: data-boot
 * @Date:   2022-04-07
 * @Version: V1.0
 */
@Api(tags="open_api_app")
@RestController
@RequestMapping("/api/openApiApp")
@Slf4j
public class OpenApiAppController extends BootController<OpenApiApp, IOpenApiAppService> {
	@Autowired
	private IOpenApiAppService openApiAppService;
	@Autowired
	private IOpenApiAppPermissionService openApiAppPermissionService;

	/**
	 * 分页列表查询
	 *
	 * @param openApiApp
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "open_api_app-分页列表查询")
	@ApiOperation(value="open_api_app-分页列表查询", notes="open_api_app-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(OpenApiApp openApiApp,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<OpenApiApp> openApiAppIPage = openApiAppService.pageList(openApiApp, pageNo, pageSize, req);
		return Result.OK(openApiAppIPage);
	}


	/**
	 *   添加
	 *
	 * @param openApiApp
	 * @return
	 */
	@AutoLog(value = "open_api_app-添加")
	@ApiOperation(value="open_api_app-添加", notes="open_api_app-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody OpenApiApp openApiApp) {
		openApiAppService.save(openApiApp);
		return Result.OK("添加成功！");
	}
	@AutoLog(value = "open_api_app-添加")
	@ApiOperation(value="授权添加", notes="授权添加")
	@PostMapping(value = "/addAuth")
	public Result<?> addAuth(@RequestBody OpenApiApp openApiApp) {
		openApiAppService.addAuth(openApiApp);
		return Result.OK("添加成功！");
	}
	/**
	 *  编辑
	 *
	 * @param openApiApp
	 * @return
	 */
	@AutoLog(value = "open_api_app-编辑")
	@ApiOperation(value="open_api_app-编辑", notes="open_api_app-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody OpenApiApp openApiApp) {
		openApiAppService.updateById(openApiApp);
		return Result.OK("编辑成功!");
	}

	 @AutoLog(value = "open_api_app-编辑")
	 @ApiOperation(value="编辑权限", notes="编辑权限")
	 @PutMapping(value = "/editAuth")
	 public Result<?> editAuth(@RequestBody OpenApiApp openApiApp) {
		 openApiAppService.editAuth(openApiApp);
		 return Result.OK("编辑成功!");
	 }


	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "open_api_app-通过id删除")
	@ApiOperation(value="open_api_app-通过id删除", notes="open_api_app-通过id删除")
	@DeleteMapping(value = "/deleteAuth")
	public Result<?> deleteAuth(@RequestParam(name="id",required=true) String id) {
		openApiAppService.deleteAuth(id);
		return Result.OK("删除成功!");
	}
	@AutoLog(value = "角色权限删除")
	@ApiOperation(value="角色权限删除", notes="角色权限删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		openApiAppService.removeById(id);
		return Result.OK("删除成功!");
	}
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "open_api_app-批量删除")
	@ApiOperation(value="open_api_app-批量删除", notes="open_api_app-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.openApiAppService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "open_api_app-通过id查询")
	@ApiOperation(value="open_api_app-通过id查询", notes="open_api_app-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		OpenApiApp openApiApp = openApiAppService.getById(id);
		if(openApiApp==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(openApiApp);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param openApiApp
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OpenApiApp openApiApp) {
        return super.exportXls(request, openApiApp, OpenApiApp.class, "open_api_app");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OpenApiApp.class);
    }

}
